﻿#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>

#define LINE	8
#define COLUMN	8
IMAGE img[6];




int map[LINE][COLUMN]={
		1 ,1 ,0 ,1 ,1,1, 1, 1,//墙 0 地板 1 箱子 2箱子目的地 3小人4命中目标5
		1 ,1, 0, 0, 0, 0, 0, 1,
		1, 1, 0, 0, 0, 0, 0, 1,
		1, 1, 0, 0, 0, 0, 0, 1,
		1, 1, 0, 0, 0, 0, 0, 1,
		1, 1, 0, 0, 0, 0, 0, 1,
		1, 3, 1, 0, 0, 0, 5, 1,
		1, 1, 1, 1, 1, 1, 1, 1,
};
void drawMap() {
	for (int i = 0; i < 8; i++) {
		for (int j = 0; j < 8; j++) {
			int x = 64 * i;
			int y = 64 * j;
			switch (map[i][j])
			{
			case 0://路
				putimage(x,y , &img[0]);
				break;

			case 1://墙
				putimage(x, y, &img[1]);
				break;
			case 3:
				putimage(x, y, &img[2]);
				break;
			case 4:
				putimage(x, y, &img[3]);
				break;
			case 5:
			case 8:
				putimage(x, y, &img[4]);
				break;
			case 7:
				putimage(x, y, &img[5]);
				break;

			}
		}printf("\n");
	}


}

void KeyDown() {
	int i, j;
	for (i = 0; i < 8; i++) {
		for (j = 0; j < 8; j++)
		{
			if (map[i][j] == 5 || map[i][j] == 8)
			{
				break;
			}
		}
		if (map[i][j] == 5 || map[i][j] == 8)
		{
			break;
		}
	}
	char userKey = _getch();
	switch (userKey)
	{
	case'a':
	case'A':
	case 72:
		if (map[i - 1][j] == 0 || map[i - 1][j] == 3)
		{
			map[i - 1][j] += 5;
			map[i][j] -= 5;
		}
		if (map[i - 1][j] == 4) {
			if (map[i - 2][j] == 0 || map[i - 2][j]==3) {
				map[i - 2][j] += 4;
				map[i][j] -= 5;
				map[i - 1][j] -= 4;
				map[i - 1][j] += 5;
			}
		}
		break;
	case'd':
	case'D':
	case 80:
		if (map[i + 1][j] == 0 || map[i + 1][j] == 3)
		{
			map[i + 1][j] += 5;
			map[i][j] -= 5;
		}
		if (map[i + 1][j] == 4) {
			if (map[i + 2][j] == 0 || map[i + 2][j] == 3) {
				map[i + 2][j] += 4;
				map[i][j] -= 5;
				map[i + 1][j] -= 4;
				map[i + 1][j] += 5;
			}
		}
		break;
		case'w':
		case'W':
		case 75:
			if (map[i ][j-1] == 0 || map[i][j-1] == 3)
			{
				map[i][j-1] += 5;
				map[i][j] -= 5;
			}
			if (map[i][j-1] == 4) {
				if (map[i][j-2] == 0 || map[i][j-2] == 3) {
					map[i][j-2] += 4;
					map[i][j] -= 5;
					map[i][j-1] -= 4;
					map[i][j-1] += 5;
				}
			}
			break;
		case's':
		case'S':
		case 77:
			if (map[i][j + 1] == 0 || map[i][j + 1] == 3)
			{
				map[i][j + 1] += 5;
				map[i][j] -= 5;
			}
			if (map[i][j +1] == 4) {
				if (map[i][j +2] == 0 || map[i][j +2] == 3) {
					map[i][j +2] += 4;
					map[i][j] -= 5;
					map[i][j +1] -= 4;
					map[i][j + 1] += 5;
				}
			}
			break;
	}
}

int main() {
	loadimage(&img[0], "flood.jfif");
	loadimage(&img[1], "wall.jpg");
	loadimage(&img[2], "rould.png");
	loadimage(&img[3], "box.png");
	loadimage(&img[4], "ren.jpg");
	loadimage(&img[5], "R-C.jfif");
	initgraph(800, 650);
	while (1) {
		drawMap();
		KeyDown();
		system("cls");
	}
	_getch();
	closegraph();
	return 0;
}